package com.yang.config;

import com.yang.pojo.Configuration;
import com.yang.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XMLMapperBuilder {

    private static XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder();
    private static Configuration configuration;

    private XMLMapperBuilder() {}
    public static XMLMapperBuilder getXmlMapperBuilder(Configuration configurati){
        configuration =configurati;
        return xmlMapperBuilder;
    }

    public void parse(InputStream inputStream) throws DocumentException {

        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        //拿到namespace
        String namespace = rootElement.attributeValue("namespace");

        List<Element> selectList = rootElement.selectNodes("//select");
        for (Element element : selectList) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String parameterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            //用来拼接唯一id
            mappedStatement.setId(id);
            //结果集类型
            mappedStatement.setResultType(resultType);
            //参数值类型
            mappedStatement.setParameterType(parameterType);
            //sql语句（未处理）
            mappedStatement.setSql(sqlText);
            //拼接全局唯一标识
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        }

        List<Element> updateList = rootElement.selectNodes("//update");
        for (Element element : updateList) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String parameterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParameterType(parameterType);
            mappedStatement.setSql(sqlText);
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        }

        List<Element> insertList = rootElement.selectNodes("//insert");
        for (Element element : updateList) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String parameterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParameterType(parameterType);
            mappedStatement.setSql(sqlText);
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        }

        List<Element> deletetList = rootElement.selectNodes("//delete");
        for (Element element : deletetList) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String parameterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParameterType(parameterType);
            mappedStatement.setSql(sqlText);
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        }

    }


}
